package com.bq;


import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

// SynchronousQueue: 同步队列
// 没有容量
// 进去一个元素，必须等待取出来之后，才能再往里面放下一个元素
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>();

        for (int i = 1; i <= 5; i++) {
            int temp = i;
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + "=>put " + String.valueOf(temp));
                    synchronousQueue.put(String.valueOf(temp));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "T1").start();
        }

        for (int i = 1; i <= 5; i++) {
            new Thread(() -> {
                try {
                    TimeUnit.SECONDS.sleep(2);
                    System.out.println(Thread.currentThread().getName() + "=>take" + synchronousQueue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "T2").start();
        }
    }
}
